package dao;

import bean.User;
import util.Dbutil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImp implements UserDao {
    @Override
    public User userLogin(User user) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //1.获取数据库连接
            connection = Dbutil.getConnection();
            //2.准备SQL语句
            String sql = "select * from t_user where userName = ? and password = ?";
            //3.执行SQL语句后获取结果并返回
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUsername());
            preparedStatement.setString(2,user.getPassword());
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                User user1 = new User(resultSet.getString("userName"),resultSet.getString("password"));
                return user1; //表示查找成功
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                //4.释放相关资源
                Dbutil.closeResource(connection,preparedStatement);
                if (null != resultSet) {
                    resultSet.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null; //表示查找失败
    }
}
